
package com.monlyu.bo;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.monlyu.bean.Page;
import com.monlyu.bo.model.HomeModel;
import com.monlyu.util.DateUtil;
import com.monlyu.util.WebOptUtil;
import com.monlyu.vo.Mail;
import com.monlyu.vo.Notice;
import com.monlyu.vo.OuterApp;

/*******************************************************************************
 * <b>功能描述☞</b><br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.bo.HomeBo.java<br>
 * <b>开发日期☞</b>2010-2-23 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 ******************************************************************************/
@Repository(value = "homeBo")
public class HomeBo extends BaseBo {

    private ProgrammingBo programmingBo;
    private ManuscriptBo manuscriptBo;
    private OuterBo outerBo;
    private SolutionBo solutionBo;

    public void setSolutionBo(SolutionBo solutionBo){
        this.solutionBo = solutionBo;
    }

    public void setOuterBo(OuterBo outerBo){
        this.outerBo = outerBo;
    }

    public void setManuscriptBo(ManuscriptBo manuscriptBo){
        this.manuscriptBo = manuscriptBo;
    }

    public void setProgrammingBo(ProgrammingBo programmingBo){
        this.programmingBo = programmingBo;
    }

    public HomeModel buildHomeModel(Page page){
        HomeModel model = new HomeModel();
        model.setNotices(getLatestNotices());
        model.setMails(getReceiveMails());
        model.setProgrammings(programmingBo.getAuditProgramming());
        model.setOuterApps(getapper(page));
        model.setMyoutApps(findmyApp());
        try {
            model.setManuscripts(manuscriptBo.getManuscripts(page).getItems());
            model.setSolutions(solutionBo.getAuditSolutions());
            model.setDrafts(manuscriptBo.getDrafts());
            model.setBacks(manuscriptBo.getBacks());
        }
        catch (Exception e) {}
        return model;
    }

    private List<OuterApp> findmyApp(){
        Criteria criteria = dao.initCriteria(OuterApp.class);
        criteria.add(Restrictions.eq("status", OuterApp.STATUS_DONE));
        criteria.add(Restrictions.ge("begin", DateUtil.nowZero()));
        criteria.add(Restrictions.or(
            Restrictions.eq("apper.id", curUser().getId()),
            Restrictions.like("joiner", "," + curUser().getId() + ",", MatchMode.ANYWHERE)));
        return criteria.list();
    }

    private List<OuterApp> getapper(Page page){
        // Criteria criteria = dao.initCriteria(OuterApp.class);
        // // criteria.add(Restrictions.eq("aduitor.id", curUser().getId()));
        // criteria.add(Restrictions.in("tvon", outerBo.getTvon()));
        // criteria.add(Restrictions.eq("status", OuterApp.STATUS_AUDITING));
        // return criteria.list();
        return outerBo.findOuts(page).getItems();
    }

    /**
     * 获得最新收到的未读邮件
     * 
     * @return
     */
    private List<Mail> getReceiveMails(){
        Criteria criteria = dao.initCriteria(Mail.class);
        criteria.add(Restrictions.eq("receiver", WebOptUtil.curUser()));
        criteria.add(Restrictions.eq("status", Mail.MAIL_STATUS_NEW));
        criteria.addOrder(Order.desc("createDate"));
        criteria.setMaxResults(10);
        return criteria.list();
    }

    /**
     * 获得最新公告
     * 
     * @return
     */
    public List<Notice> getLatestNotices(){
        Criteria criteria = dao.initCriteria(Notice.class);
        criteria.addOrder(Order.desc("createDate"));
        criteria.add(Restrictions.ge("deadDate", DateUtil.now()));
        criteria.setMaxResults(10);
        return criteria.list();
    }
}
